<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode"/>
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl"/>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName"/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
//引入js文件
import hospset from '@/api/yygh/hospset'

export default {
  data() {  //变量和初始值
    return {
      hospset:{} //封装表单提交数据对象
    }
  },
  created() { //页面渲染之前
    //路径有参数，并且id参数，获取id查询回显
    if(this.$route.params && this.$route.params.id) {
      //修改-获取路径id值，根据id查询，数据回显
      const id = this.$route.params.id
      //调用方法：根据id查询
      this.fetchDataById(id)
    }
  },
  methods:{
    //根据id查询
    fetchDataById(id) {
        hospset.getHospset(id)
          .then(response => {
            this.hospset = response.data.hospitalSet
          })
    },
    //添加
    addHospSet() {
      //添加医院设置
      hospset.save(this.hospset)
        .then(response => {
          //添加成功
          //提示信息
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          //跳转列表页面
          //路由跳转
          this.$router.push({path:'/hospset/list'})
        })
    },
    //修改
    updateHospSet() {
      hospset.update(this.hospset)
        .then(response => {
          //提示信息
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          //跳转列表页面
          //路由跳转
          this.$router.push({path:'/hospset/list'})
        })
    },
    //添加 修改
    saveOrUpdate() {
      if(!this.hospset.id) { //添加
        this.addHospSet()
      } else {  //修改
        this.updateHospSet()
      }
    }
  }
}
</script>